package com.frcdi.qms.common.utils;

import com.frcdi.qms.common.annotation.PropertyMsg;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Fgn
 * @version 1.0
 * @date 2022/1/6 10:28
 */
public class ToGetAttributeNameUtil {
    public static<T>  String toGetAttributeName(Field[] fields, T pojo1, T pojo2, StringBuilder str, Class clazz, Class clazz1){
        for(Field x : fields){
            //如果两个对象没有一个进行注解标注
            if(x.isAnnotationPresent(PropertyMsg.class)) {
                try {
                    PropertyDescriptor pd = new PropertyDescriptor(x.getName(), clazz);
                    PropertyDescriptor pd1 = new PropertyDescriptor(x.getName(), clazz1);
                    // 获取对应属性值
                    Method getMethod = pd.getReadMethod();
                    Object o1 = getMethod.invoke(pojo1);
                    Method getMethod1 = pd1.getReadMethod();
                    Object o2 = getMethod1.invoke(pojo2);
                    if (o1 == null && o2 == null) {
                        continue;
                    }
                    if (o1 == null){
                        o1="";
                    }
                    if (o2 == null){
                        o2="";
                    }
                    if (!o1.toString().equals(o2.toString())) {
                        if (StringUtils.isNotBlank(x.getAnnotation(PropertyMsg.class).enumStr())) {
                            Map<String, String> stringStringMap = Arrays.stream(x.getAnnotation(PropertyMsg.class).enumStr().split("\\|"))
                                    .map(s -> {
                                        //如果注解开头枚举为空则赋值"" 应对叠单标记等问题
                                        String[] split = s.split(",");
                                        if(split.length == 1){
                                            String[] s1 = new String[2];
                                            s1[0] = "";
                                            s1[1] = split[0];
                                            return s1;
                                        }
                                        return split;
                                    })
                                    .collect(Collectors.toMap(b -> b[0].trim(), b -> b[1].trim()));
                            o1 = stringStringMap.get(o1.toString());
                            o2 = stringStringMap.get(o2.toString());
                        }
                        if(o1 instanceof  Date){
                            o1 = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,(Date)o1);
                        }
                        if(o2 instanceof  Date){
                            o2 = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,(Date)o2);
                        }
                        String readConverterExp = x.getAnnotation(PropertyMsg.class).readConverterExp();
                        if(StringUtils.isNotEmpty(readConverterExp)){
                            if(StringUtils.isNotEmpty(o1.toString())){
                                o1 = convertByExp(o1.toString(),readConverterExp,"=");
                            }
                            if(StringUtils.isNotEmpty(o2.toString())){
                                o2 = convertByExp(o2.toString(),readConverterExp,"=");
                            }
                        }
                        if(StringUtils.isNotEmpty(o1.toString())){
                            str.append(x.getAnnotation(PropertyMsg.class).value() + ":" + " 由 \"" + o1 + "\"改为\"" + o2 + "\";");
                        }else{
                            str.append(x.getAnnotation(PropertyMsg.class).value() + ":" + o2 + "\";");
                        }

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return str.toString();
    }


    public static<T>  String toGetAttributeName(Field[] fields, T pojo1,  StringBuilder str, Class clazz  ){
        for(Field x : fields){
            //如果两个对象没有一个进行注解标注
            if(x.isAnnotationPresent(PropertyMsg.class)) {
                try {
                     PropertyDescriptor pd = new PropertyDescriptor(x.getName(), clazz);
                    // 获取对应属性值
                    Method getMethod = pd.getReadMethod();
                    Object o1 = getMethod.invoke(pojo1);
                    if (o1 == null  ) {
                        continue;
                    }
                    if (o1 == null){
                        o1="";
                    }
                    if (StringUtils.isNotBlank(x.getAnnotation(PropertyMsg.class).enumStr())) {
                        Map<String, String> stringStringMap = Arrays.stream(x.getAnnotation(PropertyMsg.class).enumStr().split("\\|"))
                                .map(s -> {
                                    //如果注解开头枚举为空则赋值"" 应对叠单标记等问题
                                    String[] split = s.split(",");
                                    if(split.length == 1){
                                        String[] s1 = new String[2];
                                        s1[0] = "";
                                        s1[1] = split[0];
                                        System.out.println( s1[1]);
                                        return s1;
                                    }

                                    return split;
                                })
                                .collect(Collectors.toMap(b -> b[0].trim(), b -> b[1].trim()));

                        o1 = stringStringMap.get(o1.toString());
                    }
                    if(o1 instanceof  Date){
                        o1 = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,(Date)o1);
                    }
                    String readConverterExp = x.getAnnotation(PropertyMsg.class).readConverterExp();
                    if(StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotEmpty(o1.toString())){
                        o1 = convertByExp(o1.toString(),readConverterExp,"=");
                    }
                    str.append( x.getAnnotation(PropertyMsg.class).value() + ":"   + o1 +   ";  ");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return str.toString();
    }

    /**
     * 解析导出值 0=男,1=女,2=未知
     *
     * @param propertyValue 参数值
     * @param converterExp 翻译注解
     * @param separator 分隔符
     * @return 解析后值
     */
    public static String convertByExp(String propertyValue, String converterExp, String separator)
    {
        StringBuilder propertyString = new StringBuilder();
        String[] convertSource = converterExp.split(",");
        for (String item : convertSource)
        {
            String[] itemArray = item.split("=");
            if (StringUtils.containsAny(separator, propertyValue))
            {
                for (String value : propertyValue.split(separator))
                {
                    if (itemArray[0].equals(value))
                    {
                        propertyString.append(itemArray[1] + separator);
                        break;
                    }
                }
            }
            else
            {
                if (itemArray[0].equals(propertyValue))
                {
                    return itemArray[1];
                }
            }
        }
        return StringUtils.stripEnd(propertyString.toString(), separator);
    }

}
